package cn.wolfcode.p2p.business.service.impl;

import cn.wolfcode.p2p.business.domain.ExpAccount;
import cn.wolfcode.p2p.business.domain.ExpAccountGrantRecord;
import cn.wolfcode.p2p.business.mapper.ExpAccountGrantRecordMapper;
import cn.wolfcode.p2p.business.service.IExpAccountFlowService;
import cn.wolfcode.p2p.business.service.IExpAccountGrantRecordService;
import cn.wolfcode.p2p.business.service.IExpAccountService;
import cn.wolfcode.p2p.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class ExpAccountGrantRecordServiceImpl implements IExpAccountGrantRecordService {
    @Autowired
    private ExpAccountGrantRecordMapper expAccountGrantRecordMapper;

    @Autowired
    private IExpAccountFlowService expAccountFlowService;

    @Autowired
    private IExpAccountService expAccountService;

    @Override
    public void saveExperienceRecord(ExpAccount expAccount,BigDecimal amount,String note,Integer expType,IExpAccountService.LastTime lastTime) {
        //保存一条体验金发放记录
        Date date = new Date();
        ExpAccountGrantRecord expAccountGrantRecord = new ExpAccountGrantRecord();
        expAccountGrantRecord.setGrantUserId(expAccount.getId());
        expAccountGrantRecord.setAmount(amount);
        expAccountGrantRecord.setGrantDate(date);
        expAccountGrantRecord.setReturnDate(lastTime.getReturnDate(date));
        expAccountGrantRecord.setGrantType(expType);
        expAccountGrantRecord.setNote(note + "[" + amount +"]元");
        expAccountGrantRecordMapper.insert(expAccountGrantRecord);
        //跟新账户
        expAccount.setUsableAmount(Constant.EXPERIENCE_THE_INITIAL_AMOUNT_OF_GOLD);
        expAccountService.update(expAccount);
        //创建收取体验金流水
        expAccountFlowService.createReceiveExperienceFlow(expAccount, Constant.EXPERIENCE_THE_INITIAL_AMOUNT_OF_GOLD,note,expType);
    }

    @Override
    public List<ExpAccountGrantRecord> listGrantRecordByState(int state) {
        return expAccountGrantRecordMapper.listGrantRecordByState(state);
    }

    @Override
    public void update(ExpAccountGrantRecord expAccountGrantRecord) {
        expAccountGrantRecordMapper.updateByPrimaryKey(expAccountGrantRecord);
    }
}
